<?php


namespace App\Models\Admin;


use App\Log\DomesticBillLog;

class DomesticBill extends Base
{
    const OPERATE_PID = 6;
    const MODEL = 'domestic_bill';

    protected $table = 'domestic_bill';
    protected $dateFormat = 'U';
    public $timestamps = true;
    protected $guarded = [];

    public function company()
    {
        return $this->belongsTo(Company::class,'company_id','id');
    }

    public static function getList($data=[],$skip=0,$size=10)
    {
        $select=self::getWhere($data);
        $list=$select->with([
            'company:id,company_name',
        ])
            ->select('id','company_id','order_sn','payment_days','send_money','commission','commission_rate','zero_money','real_money','zero_status','pay_status','pay_time','notice_days','forbid_days','created_at')
            ->orderBy('id','desc')
            ->skip($skip)->take($size)
            ->get()->transform(function ($i)
            {
                $i->company_name=$i->company->company_name;
                $i->time_left=0;
                if($i->pay_status==0)
                {
                    $i->time_left=ceil(($i->forbid_days-time())/86400);
                    $i->time_left=$i->time_left>0?$i->time_left:0;
                }
                unset($i->company);
                return $i;
            });
        return $list;
    }

    public static function getWhere($data)
    {
        $select=self::where([]);
        if(!empty($data['payment_days']))
        {
            $payment_days=date('Y-m',strtotime($data['payment_days']));
            $select=$select->where('payment_days',$payment_days);
        }
        if(!empty($data['order_sn']))
        {
            $select=$select->where('order_sn','like','%'.$data['order_sn'].'%');
        }
        if(!empty($data['company_id']))
        {
            $select=$select->where('company_id',$data['company_id']);
        }
        if(!empty($data['created_time_start']) && !empty($data['created_time_end']))
        {
            $select->whereBetween('created_at',date_assemble($data['created_time_start'],$data['created_time_end']));
        }
        if(isset($data['pay_status']))
        {
            $select=$select->where('pay_status',$data['pay_status']);
        }
        return $select;
    }

    public static function getCount($data)
    {
        $select=self::getWhere($data);
        $count=$select->count();
        return $count;
    }

    public static function getDetail($id)
    {
        $info=self::where('id',$id)
            ->with([
                'company:id,company_name',
            ])
            ->select('id','company_id','order_sn','payment_days','send_money','commission','commission_rate','zero_money','real_money','zero_status','pay_status','pay_time','notice_days','forbid_days','created_at','pay_type','collection_channels','payer','remark','credentials')
            ->first();
        if($info)
        {
            $info->company_name=$info->company->company_name;
            unset($info->company);
            $info->time_left=0;
            if($info->pay_status==0)
            {
                $info->time_left=ceil(($info->forbid_days-time())/86400);
                $info->time_left=$info->time_left>0?$info->time_left:0;
            }
            $created_time=strtotime($info->created_at->format("Y-m-d"));
            $info->forbid_days_num=intval(($info->forbid_days-$created_time)/86400);
            $info->notice_days_num=intval(($info->notice_days-$created_time)/86400);
        }

        return $info;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new DomesticBillLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
